package com.sias.bean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2023/5/31 21:41
 */
public class TestMethodTemplate {
  public static void main(String[] args) {
    MyBeanFactory beanFactory = new MyBeanFactory();
    beanFactory.addBeanPostProcessor(bean -> System.out.println("解析 @Autowired"));
    beanFactory.addBeanPostProcessor(bean -> System.out.println("解析 @Resource"));
    Object bean = beanFactory.getBean();
  }

  static class MyBeanFactory {

    public Object getBean() {
      Object bean = new Object();
      System.out.println("实例化 " + bean);
      System.out.println("依赖注入  " + bean); // @Autowired @Resource
      for (BeanPostProcessor processor : processors) {
        processor.inject(bean);
      }
      System.out.println("初始化 " + bean);
      return bean;

    }

    private List<BeanPostProcessor> processors = new ArrayList<>();

    public void addBeanPostProcessor(BeanPostProcessor processor) {
      processors.add(processor);
    }
  }

  static interface BeanPostProcessor {
    void inject(Object bean); // 对依赖注入阶段的扩展
  }


}
